package com.zuoban.config;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.zuoban.interceptor.LogTraceInterceptor;
import com.zuoban.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

@Slf4j
@EnableSwagger2WebMvc
@Configuration
public class GlobalConfig implements WebMvcConfigurer {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }

    /**
     * hutool 工具雪花id
     */
    @Bean
    public Snowflake snowflake() {
        return new Snowflake();
    }

    /**
     * knife4j
     */
    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .contact(new Contact("zxy", "http://www.zuoban.cn", "484477099@qq.com"))
                        .version("1.0")
                        .build())
                //分组名称
                .groupName("作伴平台1.0版本")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.zuoban.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public JwtUtil jwtUtil() {
        return new JwtUtil();
    }


    @Bean
    public LogTraceInterceptor logTraceInterceptor () {
        return new LogTraceInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(logTraceInterceptor());
    }
}
